from Data import search_file
import os


def model_number():
    filepaths = search_file.select_folder()
    if filepaths == "-1":
        return "-1"
    # 创建目录结构映射
    directory_map = {}
    for filepath in filepaths:
        parts = filepath.split('\\')
        if len(parts) >= 4:  # 确保路径深度足够
            first_level = parts[-3]  # 一级目录（如：命令参考）
            second_level = parts[-2]  # 二级目录（如：可靠性）
            # 初始化一级目录
            if first_level not in directory_map:
                directory_map[first_level] = {}
            # 将文件添加到对应的二级目录
            if second_level not in directory_map[first_level]:
                directory_map[first_level][second_level] = []
            directory_map[first_level][second_level].append(filepath)
    # 处理每个目录
    for first_level, second_levels in directory_map.items():
        # print(f"\033[93m处理一级目录: {first_level}\033[0m")
        for second_level, files in second_levels.items():
            for file in files:
                if "扉页" in file or "Title Page" in file:
                    continue
                else:
                    old_path = file
                    # print(old_path)
                    filename = file.rsplit('\\', 1)[-1]
                    old_path_name = file.rsplit('\\', 1)[-2]
                    # print(old_path_name)
                    if any(char.isdigit() for char in filename) and "-" in filename:
                        filename = '-'.join(filename.split('-')[1:])
                        new_path = f'{old_path_name}\\{filename}'
                        os.rename(old_path, new_path)
                        print(new_path)


if __name__ == '__main__':
    model_number()
